#include "Display.h"
#include <sstream>
#include <stdlib.h>

using namespace std;

Display::Display(){
}

Display::~Display(){
}

//display the current state of the gameBoard
void Display::displayBoard(LifeBoard gameBoard){
  for(int x = 0; x < gameBoard.getLifeBoardXRange(); x++){
    for(int y = 0; y < gameBoard.getLifeBoardYRange(); y++){
      if(gameBoard.getLifeBoardValue(x, y) == 1){
        cout << "X";
      }
      else{
        cout << ".";
      }
    }
    cout << endl;
  }
}

//display the generation number currently being displayed and
//any other relevent information
void Display::displayGenerationStats(int generation){

}

//displays the beginning menu where users will choose the next action
//that they wish to take
int Display::displayStartMenu(){
  string choice = "";

  while(choice.length() < 1 || choice.length() > 1 || choice[0] < '0'\
        || choice[0] > '9'){
  cout << "Welcome to Conway's Game of Life!" << endl << endl;
  cout << "1) Start simulation" << endl;
  cout << "2) Set custom starting values" << endl;
  cout << "3) Choose a preset starting value(Inactive)" << endl;
  cout << "4) Upload starting values from file(Inactive)" << endl;
  cout << "5) Quit" << endl << endl;
  cout << "Please choose a course of action: ";

  getline(cin, choice);
  };

  return atoi(choice.c_str());
}

int Display::getGameBoardXRange(){
  string input = "";
  bool numbers = false;

  while(!numbers){
    cout << "Input a value for the X range of the game board: ";
    getline(cin, input);
    numbers = isNumber(input);
  };

  return atoi(input.c_str());
}

int Display::getGameBoardYRange(){
  string input = "";
  bool numbers = false;

  while(!numbers){
    cout << "Input a value for the Y range of the game board: ";
    getline(cin, input);
    numbers = isNumber(input);
  };

 return atoi(input.c_str());
}

//will get the values that the user wants to use as the cells that
//are alive in the beginning.
bool Display::getStartValuesPrompt(int * startValues, int XRange, int YRange){
  string value = "";
  string test = "";
  bool numbers = false;
  bool invalid = false;
  int choice = -1;


  while(choice > XRange || choice < 0 || !numbers){
    cout << "Insert an X value: ";

    getline(cin, value);
    numbers = isNumber(value);
    if(!numbers){
      cout << "Invalid value for X. Please choose a value\n"\
              << " 0 - " << XRange << endl;
    }
    else{
      choice = atoi(value.c_str());
      if(choice < 0){
        cout << "Invalid value for X. Please choose a value\n"\
              << " 0 - " << XRange << endl;
      }
      else if(choice > XRange){
        cout << "Invalid value for X. Please choose a value\n"\
              << " 0 - " << XRange << endl;
      }
    }
  }

  startValues[0] = choice;
  numbers = false;

  while(choice > YRange || choice < 0 || !numbers){
    cout << "Insert a Y value: ";

    getline(cin, value);
    numbers = isNumber(value);
    if(!numbers){
      cout << "Invalid value for Y. Please choose a value\n"\
              << " 0 - " << YRange << endl;
    }
    else{
      choice = atoi(value.c_str());
      if(choice < 0){
          cout << "Invalid value for Y. Please choose a value\n"\
              << " 0 - " << YRange << endl;
      }
      else if(choice > XRange){
          cout << "Invalid value for Y. Please choose a value\n"\
              << " 0 - " << YRange << endl;
      }
    }
  }
  startValues[1] = choice;

  while(!invalid){
    cout << "Done picking starting seeds?(Y/N): ";
    getline(cin, test);

    if (test == "Y" || test == "y"){
      return true;
    }
    else if(test != "N" || test != "n"\
            || test != "Y" || test != "y"){
      invalid = true;
    }
    else{
    invalid = false;
    }
  }
  return false;
}

bool Display::isNumber(string value){
  for(int x = 0; x < value.length(); x++){

    if(!isdigit(value[x])){
      return false;
    }
  }

 return true;
}
